package com.wxy.ej.flow.modules.taskflow.service.nodeExecuteStrategy.strategy.controller;

import com.alibaba.fastjson.JSONObject;
import com.rt.easyjava.common.base.Result;
import com.rt.easyjava.common.utils.others.StringUtils;
import com.wxy.ej.flow.modules.taskflow.dto.DataDto;
import com.wxy.ej.flow.modules.taskflow.dto.NodeDto;
import com.wxy.ej.flow.modules.taskflow.service.nodeExecuteStrategy.NodeStrategy;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ControllerContinueNodeStrategy extends NodeStrategy {
    public static final String ContinueFlag = "MatchContinueConditionInCircle";

    @Override
    public Result action(NodeDto node, JSONObject flowDataJson, DataDto dataDto) throws Exception {
        String continueCondition = node.getContinueCondition();
        log.info("ControllerContinueNodeStrategy: continue condition: {}", continueCondition);
        dataDto.getLogList().add("条件："+continueCondition);

        boolean b = false;

        if (StringUtils.isNull(continueCondition)) {
            b = false;
        }
        else if ("false".equalsIgnoreCase(continueCondition)) {
            b = false;
        }
        else if ("true".equalsIgnoreCase(continueCondition)) {
            b = true;
        }else{
            b = (Boolean) executeScriptForResult(dataDto, continueCondition);
        }
        // 中断条件成立
        log.info("中断条件判断结果：{}", b);
        dataDto.getLogList().add("执行结果："+b);
        if (b) {
            if (dataDto.isInCircle()) {
                return Result.builder(ContinueFlag).success("中断条件符合，continue");
            } else {
                return Result.builder(ContinueFlag).fault("中断条件符合，中断流程");
            }
        }

        return Result.builder();
    }


}
